﻿var common = {};

common.mobileValidateMessage = "";
common.idCardValidateMessage = "";
common.qqValidateMessage = "";
common.zipValidateMessage = "";
common.publicationInformationValidateMessage = "";
common.wordValidateMessage = "";
common.pdfValidateMessage = "";
common.excelValidateMessage = "";

common.showError = function (errorText) {
	var lt = new RegExp("&lt;", "g"); //定义正则表达式 add by zhaoyueqi
	var gt = new RegExp("&gt;", "g"); //定义正则表达式
	errorText = errorText.replace(lt, "<");
	errorText = errorText.replace(gt, ">");
	$("#fixedErrorMessageContent").html(errorText);

	$(".fixedErrorMessage").slideDown("normal", function () {
		setTimeout(function () {
			$(".fixedErrorMessage").slideUp("normal", function () {
				$("#fixedErrorMessageContent").html("");
			});
		}, 5000);
	});
};

common.showSuccess = function (successText, autoClose) {
	$("#fixedSuccessMessageContent").html(successText);

	$(".fixedSuccessMessage").slideDown("normal", function () {
		if (autoClose) {
			setTimeout(function () {
				$(".fixedSuccessMessage").slideUp("normal", function () {
				    $("#fixedSuccessMessageContent").html("");
				});
			}, 5000);
		}
	});
};

common.setCurrentMenu = function (id) {
	$(".lmenu a#menu_" + id).addClass("s");
	$(".lmenu #img_" + id).attr("src", "../../images/icon_04.png");
};

common.setCurrentParentMenu = function (id) {
	$(id).parent("li").siblings("li").removeClass("inactives");
	$(id).addClass("inactives");
	$(id).siblings("ul").slideDown(100).children("li");
	if ($(id).parents("li").siblings("li").children("ul").css("display") == "inherit") {
		$(id).parents("li").siblings("li").children("ul").parent("li").children("a").removeClass("inactives");
		$(id).parents("li").siblings("li").children("ul").slideUp(100);
	}
};

common.formatterDate = function (date) {
	var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
	var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
	+ (date.getMonth() + 1);
	var hor = date.getHours();
	var min = date.getMinutes();
	var sec = date.getSeconds();
	return date.getFullYear() + "-" + month + "-" + day + " " + hor + ":" + min + ":" + sec;
};

$.extend($.fn.datagrid.methods, {
	autoMergeCells: function (jq, fields) {
		return jq.each(function () {
			var target = $(this);
			if (!fields) {
				fields = target.datagrid("getColumnFields");
			}

			var rows = target.datagrid("getRows");
			var i = 0, j = 0, temp = {};

			for (i; i < rows.length; i++) {
				var row = rows[i];
				j = 0;

				for (j; j < fields.length; j++) {
					var field = fields[j];
					var tf = temp[field];
					if (!tf) {
						tf = temp[field] = {};
						tf[row[field]] = [i];
					} else {
						var tfv = tf[row[field]];
						if (tfv) {
							tfv.push(i);
						} else {
							tfv = tf[row[field]] = [i];
						}
					}
				}
			}

			$.each(temp, function (field, colunm) {
				$.each(colunm, function () {
					var group = this;
					if (group.length > 1) {
						var before, after, megerIndex = group[0];

						for (var i = 0; i < group.length; i++) {
							before = group[i];
							after = group[i + 1];
							if (after && (after - before) == 1) {
								continue;
							}

							var rowspan = before - megerIndex + 1;

							if (rowspan > 1) {
								target.datagrid("mergeCells", {
									index: megerIndex,
									field: field,
									rowspan: rowspan
								});
							}
							if (after && (after - before) != 1) {
								megerIndex = after;
							}
						}
					}
				});
			});
		});
	}
});

Array.prototype.remove = function () {
	var what, a = arguments, L = a.length, ax;
	while (L && this.length) {
		what = a[--L];
		while ((ax = this.indexOf(what)) !== -1) {
			this.splice(ax, 1);
		}
	}
	return this;
};

function compute(colName, gridName) {
	var grid = gridName;
	var rows = grid.datagrid("getRows");
	var total = 0;
	for (var i = 0; i < rows.length; i++) {
		total += parseFloat(rows[i][colName]);
	}

	return Math.round(total * 100) / 100;
}

common.addAffliation = function () {
	for (var index = 0; index < 10; index++)
	{
		if ($("#trAffiliation" + index).css("display") == "none")
		{
			$("#trAffiliation" + index).css("display", "");
			break;
		}
	}	
};